var path = require('path');
var express = require('express');
var webpack = require('webpack');
var webpackHotMiddleware = require('webpack-hot-middleware')
var config = require('./webpack.config.dev');


var proxyMiddleWare = require("http-proxy-middleware");
// var proxyPath = "http://test.betterokr.com";//目标后端服务地址
var proxyPath = "http://localhost:8180";//目标后端服务地址
var proxyOption ={target:proxyPath,changeOrigoin:true};
var proxyObj = proxyMiddleWare(proxyOption);

var app = express();
var port = process.env.PORT || 8000
var compiler = webpack(config);

app.use('/static', express.static(__dirname));

app.use("/auths",proxyObj);
app.use("/users",proxyObj);
app.use("/usernames",proxyObj);
app.use("/profile-templates",proxyObj);
app.use("/okrs",proxyObj);
app.use("/documents",proxyObj);
app.use("/avatars",proxyObj);
app.use("/sidebars",proxyObj);
app.use("/report-lines",proxyObj);
app.use("/teams",proxyObj);
app.use("/addresses",proxyObj);
app.use("/onboarding-flows",proxyObj);
app.use("/onboarding-templates",proxyObj);
app.use("/feeds",proxyObj);
app.use("/reviews",proxyObj);
app.use("/employ-managements",proxyObj);
app.use("/remind-settings",proxyObj);
app.use("/org",proxyObj);
app.use("/role-permissions",proxyObj);
app.use("/roles",proxyObj);
app.use("/messages",proxyObj);
app.use("/name-indexes",proxyObj);
app.use("/navigation",proxyObj);
app.use("/rewards",proxyObj);
app.use("/surveys",proxyObj);
app.use("/conversations",proxyObj);
app.use("/teams_project",proxyObj);

app.use(require('webpack-dev-middleware')(compiler, {
    noInfo: true,
    publicPath: config.output.publicPath,
    host: '0.0.0.0',
}));
app.use(webpackHotMiddleware(compiler));

[
    '/'
].forEach(function(_path) {
    app.get(_path, function(req, res, next) {
        res.sendFile(path.join(__dirname, 'index.html'));
    });
});

[
    '/ui'
].forEach(function(_path) {
    app.get(_path, function(req, res, next) {
        res.sendFile(path.join(__dirname, 'ui.html'));
    });
});

[
    '/util'
].forEach(function(_path) {
    app.get(_path, function(req, res, next) {
        res.sendFile(path.join(__dirname, 'util.html'));
    });
});

[
    '/u'
].forEach(function(_path) {
    app.get(_path, function(req, res, next) {
        res.sendFile(path.join(__dirname, 'auth.html'));
    });
});

[
    '/admin'
].forEach(function(_path) {
    app.get(_path, function(req, res, next) {
        res.sendFile(path.join(__dirname, 'admin.html'));
    });
});
[
    '/charts'
].forEach(function(_path) {
    app.get(_path, function(req, res, next) {
        res.sendFile(path.join(__dirname, 'charts.html'));
    });
});

[
    '/onboarding-flows/staff'
].forEach(function(_path) {
    app.get(_path, function(req, res, next) {
        res.sendFile(path.join(__dirname, 'onboarding.html'));
    });
});

app.listen(port, '0.0.0.0', function(error) {
    if (error) {
        console.error(error)
    } else {
        console.info("==> 🌎  Listening on port %s. Open up http://localhost:%s/ in your browser.", port, port)
    }
})
